<?php

class wapWordPress {
	var $adminOptionsName = "WapWordPressAdminOptions";
	var $siteRoot					= '';
	var $wwRoot						= '';
  
	function wapWordPress() {
//		$this->$siteRoot	= get_option('home');
		$this->siteRoot	= str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
//		$this->$wwRoot		= 
		return 0;
	}

	//Returns an array of admin options
	function getAdminOptions() {
		$wwAdminOptions = array('ww_is_on' => '', 'ww_wap_path' => '/wap', 'ww_ad_on' => '', 'ww_wap_pwd_key' => '');
		$wwOptions = get_option($this->adminOptionsName);
		if (!empty($wwOptions)) {
				foreach ($wwOptions as $key => $option)
						$wwAdminOptions[$key] = $option;
		}
		update_option($this->adminOptionsName, $wwAdminOptions);
		return $wwAdminOptions;
	}

	function updateAdminOptions($ww_options) {
			$wwAdminOptions['ww_is_on']	=	$ww_options['ww_is_on'];
			$wwAdminOptions['ww_wap_path']	=	$ww_options['ww_wap_path'];
			$wwAdminOptions['ww_ad_on']	=	$ww_options['ww_ad_on'];
			$wwAdminOptions['ww_wap_pwd_key']	=	$ww_options['ww_wap_pwd_key'];
			update_option($this->adminOptionsName, $wwAdminOptions);
			$this->createIndex();

			$ad_code	= trim($ww_options['ww_ad']);
			$ad_code	= str_replace('\\\'', '\'', $ad_code);
			$fid	= fopen($this->getADPath(),'w');
			if($fid){
				fwrite($fid,$ad_code);
				fclose($fid);
			}
	}

	function clrAdminOptions() {
			delete_option($this->adminOptionsName);
	}

	//return adsense code file path
	function getADPath() {
		$adpath	= dirname(__FILE__).'/../files/ad-wap-code.php';
		$adpath	= str_replace('\\', '/', $adpath);
		return $adpath;
	}

	//创建WAP根目录
	function createPath($path){
		if(!is_dir($path)){
			$parent	= dirname($path);
			if(!is_dir($parent)){
				$this->createPath($parent);
			}
			else{
				mkdir($path);
			}
		}
	}

	//创建WAP页面
	function createIndex() {
		$wwOptions = get_option($this->adminOptionsName);
		
		if($wwOptions['ww_is_on']	==	'on') {
			$index_path		=	$this->siteRoot.$wwOptions['ww_wap_path'];
			$header_path	= str_replace('func', 'files', dirname(__FILE__)).'/header.php';
			$header_path	= str_replace('\\', '/',$header_path);

			$this->createPath($index_path);
			$file	= $index_path.'/index.php';
			$str	=	'<?php header(\'Content-type:text/vnd.wap.wml; charset=utf-8\'); ?><?php require_once(\''.$header_path.'\'); ?>';
			$fid	= fopen($file,'w');
			if($fid){
				fwrite($fid,$str);
				fclose($fid);
			}
		}

	}


	function init() {
		$this->getAdminOptions();
	}

	function uninit() {
		$this->clrAdminOptions();
	}


}



?>